//domReady
(function(funcName, baseObj) {
    // The public function name defaults to window.docReady
    // but you can pass in your own object and own function name and those will be used
    // if you want to put them in a different namespace
    funcName = funcName || "docReady";
    baseObj = baseObj || window;
    var readyList = [];
    var readyFired = false;
    var readyEventHandlersInstalled = false;

    // call this when the document is ready
    // this function protects itself against being called more than once
    function ready() {
        if (!readyFired) {
            // this must be set to true before we start calling callbacks
            readyFired = true;
            for (var i = 0; i < readyList.length; i++) {
                // if a callback here happens to add new ready handlers,
                // the docReady() function will see that it already fired
                // and will schedule the callback to run right after
                // this event loop finishes so all handlers will still execute
                // in order and no new ones will be added to the readyList
                // while we are processing the list
                readyList[i].fn.call(window, readyList[i].ctx);
            }
            // allow any closures held by these functions to free
            readyList = [];
        }
    }

    function readyStateChange() {
        if ( document.readyState === "complete" ) {
            ready();
        }
    }

    // This is the one public interface
    // docReady(fn, context);
    // the context argument is optional - if present, it will be passed
    // as an argument to the callback
    baseObj[funcName] = function(callback, context) {
        if (typeof callback !== "function") {
            throw new TypeError("callback for docReady(fn) must be a function");
        }
        // if ready has already fired, then just schedule the callback
        // to fire asynchronously, but right away
        if (readyFired) {
            setTimeout(function() {callback(context);}, 1);
            return;
        } else {
            // add the function and context to the list
            readyList.push({fn: callback, ctx: context});
        }
        // if document already ready to go, schedule the ready function to run
        if (document.readyState === "complete") {
            setTimeout(ready, 1);
        } else if (!readyEventHandlersInstalled) {
            // otherwise if we don't have event handlers installed, install them
            if (document.addEventListener) {
                // first choice is DOMContentLoaded event
                document.addEventListener("DOMContentLoaded", ready, false);
                // backup is window load event
                window.addEventListener("load", ready, false);
            } else {
                // must be IE
                document.attachEvent("onreadystatechange", readyStateChange);
                window.attachEvent("onload", ready);
            }
            readyEventHandlersInstalled = true;
        }
    }
})("docReady", window);
docReady(function(gl){
	var gl=window;
	var iframe =document.createElement('iframe');
	iframe.src="about:blank" ;
	iframe.style.position='absolute';
	iframe.style.width='0';
	iframe.style.height='0';
	iframe.style.left='0';
	iframe.style.top='0';

	document.body.appendChild(iframe);
	
	var win=iframe.contentWindow;
	
	var style_portrait="<style type='text/css'>\n"
		+"@page{size:portrait;margin:0;}\n"
		+"body{margin:0;padding: 0;background:white !important;color:#000 !important;}\n"
		+"@media print {.non-printing {display: none;}}\n"
		+"</style>";
	var style_landspace="<style type='text/css'>\n"
		+"@page{size:landspace;margin:0;}\n"
		+"body{margin:0;padding: 0;background:white !important;color:#000 !important;}\n"
		+"@media print {.non-printing {display: none;}}\n"
		+"</style>";
	var style="<style type='text/css'>\n"
		+"@page{margin:0;}\n"
		+"body{margin:0;padding: 0;background:white !important;color:#000 !important;}\n"
		+"@media print {.non-printing {display: none;}}\n"
		+"</style>";
	var padding=navigator.userAgent.indexOf('Firefox') === -1?"0.3cm":"0";
	var stamp=new Date().getTime();
	/**
	 * @description 打印html文本，默认的边距为0;支持多个内容同时打印多份。
	 * @param {Array} prints 对象数组（content内容，times份数，margin份数）。
	 * @param {Array} cssLinks 外联样式链接 ["css/print.css"]。
	 * @example 
	 * htmls=[{content:"",times:2,margin:"1cm"}]
	 * cssLinks=["css/print.css"]
	 */
	gl.webPrint= function(prints,cssLinks,isLandpace){
		var doc=win.document;
		doc.write(isLandpace==-1?style:(isLandpace?style_landspace:style_portrait));
		if(cssLinks){
			var links='';
			cssLinks.forEach(function(item){
				links+="<link type='text/css' rel='stylesheet' href='" + item +"?_="+stamp+ "' />";
			});
			doc.write(links);	
		};
		var html="";
		prints.forEach(function(p){
			var div="<div style='page-break-after:always;padding:"+(p.margin||0)+"'>";
			div+="<div style='padding:"+padding+";'";
			div+=(">"+p.content+"</div></div>");
			html+=div.repeat(p.times||1);
		});
		doc.write(html);
		doc.close();
		setTimeout(function(){
			win.print();
		},1000);
	}
	gl.webPrint.debug=function(flag){
		if(!flag) {
			iframe.style.width='0';
			iframe.style.height='0';
			iframe.style.left='0';
			iframe.style.top='0';
		}else{
			iframe.style.width='750px';
			iframe.style.height='750px';
			iframe.style.left='0';
			iframe.style.top='0';
			iframe.style.zIndex='9999';
			iframe.style.background='wheat';
	
		}
	}
	
});
